package com.boxuegu.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.boxuegu.R;
import com.boxuegu.VideoPlayActivity;
import com.boxuegu.bean.VideoBean;
import com.boxuegu.utils.DBAccess;
import com.boxuegu.utils.SPreadOrWrite;

import java.util.List;

public class VideoListAdapter extends BaseAdapter {

    private AppCompatActivity context;
    private List<VideoBean> beans;
    private ViewHolder oldLine;


    public VideoListAdapter(AppCompatActivity context, List<VideoBean> beans) {
        this.beans=beans;
        this.context=context;
        this.oldLine=null;
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int position) {
        return beans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=new ViewHolder();
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.video_list_item,null);
            viewHolder.tv_right=convertView.findViewById(R.id.tv_right);
            viewHolder.iv_left=convertView.findViewById(R.id.iv_left);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder)convertView.getTag();
        }

        viewHolder.tv_right.setText(beans.get(position).videoTitle);

        final ViewHolder vh=viewHolder;

        viewHolder.iv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listenClick(vh,position);
            }
        });

        viewHolder.tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listenClick(vh,position);
            }
        });

        return convertView;
    }

    private void listenClick(ViewHolder current,int position){//current:当前选择的行控件
        //复原原来行
        if(oldLine!=null){
            oldLine.iv_left.setImageResource(R.drawable.course_bar_icon);
            oldLine.tv_right.setTextColor(Color.parseColor("#333333"));
        }
        //变色选择行
        current.iv_left.setImageResource(R.drawable.course_intro_icon);
        current.tv_right.setTextColor(Color.parseColor("#009958"));
        oldLine=current;//当前选择行，下次选择时就变成了前面选择的行

        //选择某行时，要跳转到视频播放页面，携带该视频的地址
        if(beans.get(position).videoPath!=null&&!beans.get(position).videoPath.equals("")) {
            //该条件成立，表示视频存在
            if(SPreadOrWrite.readLoginStatus(context)){//表示当前有用户已经登录，保存播放记录
                String userName=SPreadOrWrite.readLoginName(context);
                //接下来将播放记录保存到数据库表中：用户名，视频id，视频地址，视频名字
                DBAccess dbAccess=DBAccess.getDBAccessObject(context);
                dbAccess.saveVideoPlayList(SPreadOrWrite.readLoginName(context),beans.get(position));
            }
            Intent intent = new Intent(context, VideoPlayActivity.class);
            intent.putExtra("videoPath", beans.get(position).videoPath);//携带地址
            context.startActivity(intent);
        }else {
            Toast.makeText(context,"视频不存在",Toast.LENGTH_LONG).show();
        }
    }

    class ViewHolder{
        public ImageView iv_left;
        public TextView tv_right;
    }
}
